package com.threeonemeeting.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.threeonemeeting.R;
import com.threeonemeeting.model.IconItemInfo;

public class IconItemListAdapter extends ArrayAdapter<IconItemInfo> {

	private int mnLayoutId = 0;
	private int mnItemHeight = 0;

	private ArrayList<IconItemInfo> mItemsList = null;

	public IconItemListAdapter(Activity activity) {
		super(activity, 0);

		mnLayoutId = R.layout.lvi_icon_item;
	}

	public IconItemListAdapter(Activity activity, int mnLayoutId) {
		super(activity, 0);

		this.mnLayoutId = mnLayoutId;
	}

	public void setMinHeight(int nMinHeight) {
		mnItemHeight = nMinHeight;
	}

	public void setIconList(ArrayList<IconItemInfo> iconList) {
		mItemsList = iconList;
		notifyDataSetChanged();
	}

	public ArrayList<IconItemInfo> getIconList() {
		return mItemsList;
	}

	@Override
	public void add(IconItemInfo iconInfo) {
		if (mItemsList == null)
			mItemsList = new ArrayList<IconItemInfo>();

		mItemsList.add(iconInfo);
	}

	public void clear() {
		if (mItemsList != null) {
			mItemsList.clear();
		}
	}

	@Override
	public int getCount() {
		if (mItemsList == null)
			return 0;

		return mItemsList.size();
	}

	@Override
	public IconItemInfo getItem(int position) {
		return mItemsList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		Activity activity = (Activity) getContext();

		IconItemInfo itemInfo = getItem(position);

		View rowView = convertView;
		IconItemInfoCache viewCache;
		if (rowView == null) {
			LayoutInflater inflater = activity.getLayoutInflater();
			rowView = inflater.inflate(mnLayoutId, null);

			viewCache = new IconItemInfoCache(rowView, itemInfo.nIconResId,
					itemInfo.strText);
			rowView.setTag(viewCache);
		} else {
			viewCache = (IconItemInfoCache) rowView.getTag();
		}

		ImageView itemIconImageView = viewCache.getIconImageView();
		itemIconImageView.setBackgroundResource(itemInfo.nIconResId);

		// 会议标题
		TextView itemIconTextView = viewCache.getItemTitleTextView();
		itemIconTextView.setText(itemInfo.strText);

		if (mnItemHeight != 0) {
			rowView.setMinimumHeight(mnItemHeight);
		}

		return rowView;
	}

	public class IconItemInfoCache {

		private View baseView;

		private int nItemResId;
		private String strItemText;

		private ImageView itemIconImageView;
		private TextView itemTitleTextView;

		public IconItemInfoCache(View baseView, int nIconResId, String strText) {
			this.baseView = baseView;

			this.nItemResId = nIconResId;
			this.strItemText = strText;
		}

		public int getItemIconResId() {
			return this.nItemResId;
		}

		public String getItemText() {
			return this.strItemText;
		}

		public ImageView getIconImageView() {
			if (itemIconImageView == null) {
				itemIconImageView = (ImageView) baseView
						.findViewById(R.id.item_icon);
			}
			return itemIconImageView;
		}

		public TextView getItemTitleTextView() {
			if (itemTitleTextView == null) {
				itemTitleTextView = (TextView) baseView
						.findViewById(R.id.item_text);
			}
			return itemTitleTextView;
		}
	}
}
